package com.cz.mes;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.db.Db;
import cn.hutool.db.Entity;

import java.io.File;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class HandleRepeatOrder {
    public static void main(String[] args) {
        try {
            String fileName = "HandleRepeatOrder"+ DateUtil.format(DateUtil.date(), "yyyy-MM-dd");
            File logFile = new File(fileName + ".log");

            List<String> logList = new ArrayList<>();

            String dbgroup = "mes-prod-master";     //mes-prod-master
            List<Entity> entityList = Db.use(dbgroup).query("select bill_no,count(*) as total from manufacture_order where status=1 group by bill_no having count(*) > 1");
            System.out.println(entityList.size());
            for (Entity entity : entityList) {
                String billNo = entity.getStr("bill_no");
                try {
                    Entity repeat = Db.use(dbgroup).queryOne("select id,bill_no from manufacture_order where bill_no =?", billNo);

                    int id = repeat.getInt("id");
                    String bill_no = repeat.getStr("bill_no");
                    System.out.println(id + " " + bill_no);

                    String new_bill_no = bill_no + "-1";

                    int result = Db.use(dbgroup).execute("update manufacture_order set bill_no = ?  where id=?", new_bill_no, id);
                    System.out.println("更新结果：" + result);
                    String record = "manufacture_order" + " " + id + " " + bill_no;
                    logList.add(record);

                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }

            if(CollUtil.isNotEmpty(logList)){
                FileUtil.writeLines(logList, logFile, "utf-8",true);

            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
